package org.example.Server.Impl;

import org.example.Exception.EmpExistException;
import org.example.Mapper.DepthMapper;
import org.example.Mapper.EmpMapper;
import org.example.Pojo.Depth;
import org.example.Pojo.Emp;
import org.example.Server.DepthServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DepthServerImpl implements DepthServer {

    @Autowired
    private DepthMapper depthMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Depth> findAll() {
        return depthMapper.findAll();
    }

    @Transactional
    @Override
    public void deleteById(Integer id) throws EmpExistException {
        List<Emp> allEmp = empMapper.getAllEmp();
        for (Emp emp : allEmp) {
            if (emp.getDeptId().equals(id)) {
                //说明不能删除
                throw new EmpExistException();
            }
        }
        depthMapper.deleteById(id);
    }

    @Override
    public void insert(Depth depth) {
        depth.setCreateTime(LocalDateTime.now());
        depth.setUpdateTime(LocalDateTime.now());
        depthMapper.insert(depth);
    }

    @Override
    public Depth findById(Integer id) {
        Depth depth=depthMapper.findById(id);
        return depth;
    }

    @Override
    public void updateById(Depth depth) {
        depthMapper.updateById(depth);
    }
}
